import { Request, toast, formDate } from '../../utils/util.js'
let request = new Request()
let app = getApp()
Component({

  /**
   * 页面的初始数据
   */
  data: {
    isFirst: true,
    scrollTop : false,
    cartList : [],
    scene : 1,
    goodsCount : 0,
    isAllSelected : false,
    amount : 0,
    cartIds : []
  },
  methods : {
    toggleScene() {
      this.setData({ scene: this.data.scene == 1 ? 2 : 1 })
    },

    changeCart(e) {
      let quantity = e.detail
      if (quantity <= 0) {
        toast('最少购物一件~')
        return
      }
      let dataset = e.currentTarget.dataset
      let index = dataset.index
      let idx = dataset.idx
      let update = {}
      let cart = this.data.cartList[index].cart[idx]
      if (cart.quantity == quantity) {
        return
      }
      request.post('cart/change', res => {
        if (res.success) {
          let update = {}
          update[`cartList[${index}].cart[${idx}].quantity`] = quantity
          this.setData(update)
        } else {
          toast(res.msg)
        }
      }, { id: cart.id, quantity: quantity }).showLoading()
    },

    select(e) {
      let id = e.currentTarget.dataset.id
      let goodsCount = this.data.goodsCount
      let isAllSelected = this.data.isAllSelected
      let cartIds = new Set(this.data.cartIds)
      if (e.detail.value) {
        cartIds.add(id)
      } else {
        cartIds.delete(id)
      }

      cartIds = Array.from(cartIds)
      this.setData({ cartIds: cartIds })
      console.log(cartIds.length, goodsCount)
      if (cartIds.length >= goodsCount) {
        this.setData({ isAllSelected: true })
      } else {
        if (isAllSelected) {
          this.setData({ isAllSelected: false })
        }
      }

      this.statisticsAmount()
    },

    allSelect(e) {
      let checked = e.detail.value
      let cartIds = new Set()

      let cartList = this.data.cartList
      cartList.forEach((list, index) => {
        list.cart.forEach((v, i) => {
          if (checked) {
            cartIds.add(v.id)
          }
          let update = {}
          update[`cartList[${index}].cart[${i}].checked`] = checked
          this.setData(update)
        })
      })
      if (checked) {
        cartIds = Array.from(cartIds)
        this.setData({ cartIds: cartIds, isAllSelected: true })
      } else {
        this.setData({ cartIds: [] })
      }
      this.statisticsAmount()
    },

    delCart() {
      let cartIds = this.data.cartIds
      if (cartIds.length == 0) {
        toast('没有选择商品呢~')
        return
      }

      request.post('cart/delete', res => {
        if (res.success) { //删除成功
          let cartList = this.data.cartList
          let _cartList = []
          cartList.forEach((list, index) => {
            let cart = {
              id: index,
              store: list.store,
              cart: []
            }
            list.cart.forEach((v, i) => {
              if (!cartIds.includes(v.id)) {
                v.id = i
                cart.cart.push(v)
              }
            })
            if (cart.cart.length != 0) {
              _cartList.push(cart)
            }

          })
          this.setData({ cartList: _cartList, cartIds: [], isAllSelected: false })
          this.statisticsAmount()
        }
      }, { cartIds: cartIds })

    },

    statisticsAmount() {
      let amount = 0
      let cartIds = this.data.cartIds
      if (cartIds.length != 0) {
        let cartList = this.data.cartList
        cartList.forEach(item => {
          item.cart.forEach(v => {
            if (cartIds.includes(v.id)) {
              amount += (v.sale_price * v.quantity)
            }

          })
        })
      }

      this.setData({ amount: amount })
    },

    checkout() {
      let cartIds = this.data.cartIds
      if (cartIds.length == 0) {
        toast('还没选择商品呢~')
        return
      }

      cartIds = cartIds.join(',')
      wx.navigateTo({
        url: '/pages/checkout/index?cartIds=' + cartIds
      })
    },

    onLoad: function (options) {

    },

    onShow: function () {
      request.get('cart', res => {
        if (res.success) {
          let goodsCount = 0
          let cartList = res.data.list
          if (cartList.length == 0) {
            this.setData({ cartList: null })
            return
          }
          cartList.forEach((v, index) => {
            v.store = JSON.parse(v.store)
            v.id = index
            v.cart.forEach(item => {
              item.checked = false
              goodsCount++
            })
          })
          this.setData({ cartList: cartList, goodsCount: goodsCount })
        }
      }).showLoading()
    }
  },
  pageLifetimes: {
    show() {
      if (this.data.isFirst) {
        this.setData({ isFirst: false })
      } else {
       
        
      }

      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selectedIndex: 3
        })
      }
    }
  }
})